package com.example.listener.page

import android.bluetooth.BluetoothAdapter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.listener.databinding.FragmentBluetoothListBinding
import com.example.listener.databinding.ItemBluetoothListBinding
import com.example.listener.utils.XDiffCallback
import com.seed.launcher.listener.bean.BluetoothInfo
import com.seed.launcher.listener.bluetooth.BluetoothListener
import com.seed.launcher.listener.bluetooth.OnBluetoothChangListener

/**
 *
 * @author xct
 * create on: 2022/9/1 15:13
 *
 */
class BluetoothListFragment : Fragment() {


    private lateinit var viewBinding: FragmentBluetoothListBinding
    private val mAdapter by lazy { BluetoothAdapter() }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        return FragmentBluetoothListBinding.inflate(inflater, container, false)
            .also { viewBinding = it }
            .root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinding.apply {
            recyclerView.layoutManager = LinearLayoutManager(requireContext())
            recyclerView.adapter = mAdapter
        }
        val listener = BluetoothListener(requireContext(),object :OnBluetoothChangListener{
            override fun onDiscoveryStart() {
                viewBinding.progressBar.isVisible = true
            }

            override fun onDiscoveryFinished() {
                viewBinding.progressBar.isVisible = false
            }

            override fun onBluetoothListChanged(list: MutableList<BluetoothInfo>?) {
                val it = list?:return
                mAdapter.submitList(it)
                viewBinding.tvInfo.text = "bluetooth count:${it.size}"
            }
        })
        viewLifecycleOwner.lifecycle.addObserver(object :DefaultLifecycleObserver{
            override fun onCreate(owner: LifecycleOwner) {
                super.onCreate(owner)
                listener.register()
            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                listener.unregister()
            }
        })
    }

    inner class ViewHolder(val vb:ItemBluetoothListBinding):RecyclerView.ViewHolder(vb.root)
    inner class BluetoothAdapter :ListAdapter<BluetoothInfo,ViewHolder>(XDiffCallback()){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(ItemBluetoothListBinding.inflate(LayoutInflater.from(parent.context)))
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val item: BluetoothInfo = getItem(position)
            holder.vb.apply {
                tvInfo.text = item.toString()
            }
        }
    }
}